package com.file.transfers.socket;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import com.file.transfers.util.FileUtil;

public class FileTransfersClient {
	private String IP;
	private int	port;
	public FileTransfersClient(String IP,int port){
		this.IP=IP;
		this.port=port;
	}
	private static String separator="/";
	public static List<String> getFileLists(List<String> list,String path,boolean isDiGui){
		if(list==null)list=new ArrayList<String>();
		File file=new File(path);
		if(file.isFile()){
			list.add(file.getAbsolutePath());
			return list;
		}
		File[] listFiles = file.listFiles();
		 for (int i = 0; i < listFiles.length; i++) {
			 File tmpFile=listFiles[i];
			 String absolutePath = tmpFile.getAbsolutePath();
			 if(tmpFile.isFile()){
				 list.add(absolutePath);
				 System.out.print(absolutePath);
			 }else{
				 if(isDiGui) getFileLists(list,absolutePath,isDiGui);
			 };
			
		 }
		return list;
		
	}
	public static String splitFileName(String basePath,String filePath,String separator){
		String splitfilePath="";
		if(!basePath.equals(filePath)){
			splitfilePath= filePath.substring(basePath.length()+1);
		}
		//System.out.println(splitfilePath);
		return splitfilePath;
		
	}
	private void sendFiles(String path,String remoteIP,int remotePort){
		boolean isDiGui=true;
		Socket socket=null;
		try {
			 socket=new Socket(remoteIP, remotePort);
			 OutputStream out = socket.getOutputStream();
			
			 ObjectOutputStream objectOutputStream=new ObjectOutputStream(out);
			 objectOutputStream.write('a');
			 String fileNames="";
			 List<String> fileLists = getFileLists(null,path, isDiGui);
			 for (int i = 0; i < fileLists.size(); i++) {
				 String name = fileLists.get(i);
				 if(i==fileLists.size())fileNames+=name;
				 if(i!=fileLists.size())fileNames+=name+",";
			 }
			 System.out.println("Client:一共有"+fileLists.size()+"文件，需要传输。");
			 objectOutputStream.writeUTF(fileNames);
			 for (int i = 0; i < fileLists.size(); i++) {
				 System.out.println("Client:目前正在传输"+(i+1)+"文件");
				 File file=new File(fileLists.get(i));
				//if(!file.isFile())break;
				 String name = file.getName();
				 String splitFileName = splitFileName(path, file.getParent(),separator);
				 long totalSpace = file.length();
				 //发送之前 先发送一个字节的数据 应为server是通过objectInputStream.read() 阻塞来实现的，先读取一个字节。
				 if(i>0)objectOutputStream.write('a'); 
				 objectOutputStream.writeUTF(splitFileName+"#"+name);
				 objectOutputStream.flush();
				 objectOutputStream.writeLong(totalSpace);
				 objectOutputStream.flush();
				 FileInputStream fileInputStream=new FileInputStream(file);
				 
				 int length=3*1024*1024;
				 if(totalSpace<1024)length=(int) totalSpace;
				 byte[] b=new byte[length];
				 System.out.println("Client:文件名："+name+",文件大小："+totalSpace+"");
				 int read = fileInputStream.read(b);
				 boolean outputShutdown = socket.isOutputShutdown();
				 System.out.println("outputShutdown:"+outputShutdown);
				 int a=0;
				 while(read!=-1){
					 System.out.println(read);
					 objectOutputStream.write(b,0,read);
					 objectOutputStream.flush();
					 read=fileInputStream.read(b);
				 };
				 objectOutputStream.flush();
				 fileInputStream.close();
				 objectOutputStream.flush();
				 System.out.println("Client:"+(i+1)+"文件传输完毕");
				
			}
			 socket.shutdownOutput();
			 socket.close();
			 socket=null;
			 System.out.println("Client:发送完成。");
		}catch (Exception e) {
			e.printStackTrace();
			 try {
				if(socket!=null)socket.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			 socket=null;
			
			System.out.println("Client:网络通信出现异常："+e.getMessage());
		}
	
		
	}
	public int preSendFiles(String filePath){
		int flag=0;
		boolean exists = FileUtil.isExists(filePath);
		if(exists){
			sendFiles(filePath, this.IP, this.port);
			flag=0;
		}else{
			flag=1;//发送目录不存在
		}
		return flag;
		
	}
	public static void main(String[] args) {
		//String path="D:\ftp资料\设计模式";
		String path="D:\\ftp资料\\设计模式";
		String ip="22.11.148.186";
		int port=8888;
		FileTransfersClient fileTransfersClient=new FileTransfersClient(ip,port);
		fileTransfersClient.preSendFiles(path);
	}
}
